package io.michaelrocks.libphonenumber.ohos.sample.slice;

import io.michaelrocks.libphonenumber.ohos.PhoneNumberUtil;
import io.michaelrocks.libphonenumber.ohos.Phonenumber;
import io.michaelrocks.libphonenumber.ohos.sample.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.ToastDialog;

public class MainAbilitySlice extends AbilitySlice {
    private PhoneNumberUtil util = null;
    private Text text ;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        text = (Text) findComponentById(ResourceTable.Id_text_helloworld);
        Button button = (Button) findComponentById(ResourceTable.Id_button);
        // 为按钮设置点击事件回调
        button.setClickedListener(component -> {
            // 此处添加点击按钮后的事件处理逻辑
            if (util == null) {
                util = PhoneNumberUtil.createInstance(getAbilityPackageContext());
            }
            try {
                final Phonenumber.PhoneNumber phoneNumber = util.parse("0298123456", "CN");
                text.setText(util.format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL));
            } catch (Exception e) {
                ToastDialog toastDialog = new ToastDialog(getContext());
                toastDialog.setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT);
                toastDialog.setText(e.toString());
                Text toastText=(Text) toastDialog.getComponent();
                if(toastText!=null)
                {
                    toastText.setMultipleLine(true);
                    toastText.setTextSize(14, Text.TextSizeType.FP);
                    toastText.setPaddingBottom(20);
                    toastText.setPaddingTop(20);
                }
                toastDialog.show();
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
